package com.glela.common.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class LinkUtil {
private static String baidu_api_url = "http://dwz.cn/create.php";
	
	//下面网址有很多备用的新浪APPKEY  可直接使用
	//https://www.cnblogs.com/duanhuajian/p/3834044.html
	private static String sina_app_key = "3271760578";
	private static String sina_api_url = "http://api.t.sina.com.cn/short_url/shorten.json?source=" + sina_app_key + "&url_long=";
	
	/***
	 * 
	 * @param long_url : 通过长链接获取百度短链接
	 * @return
	 */
	public static String generateBaiDuShortUrl(String long_url) {
		try {
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("url", long_url);
			String respString = HttpUtil.post(baidu_api_url, paramMap);
			System.out.println(respString);
			JSONObject jsonObject = JSONObject.parseObject(respString);
			if(StringUtils.isEmpty(jsonObject.getString("tinyurl"))) {
				return long_url;
			}
			long_url = jsonObject.getString("tinyurl");
			return long_url;
		} catch (Exception e) {
			return long_url;
		}
	}
	
	
	/***
	 * 
	 * @param long_url : 通过长链接获取新浪短链接
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String generateSinaShortUrl(String long_url){
		String ret = null;
		try {
			if(true)return long_url;
			if(org.apache.commons.lang3.StringUtils.isNotBlank(long_url)) {
				String base64Url = URLEncoder.encode(long_url,"utf-8");
				ret = HttpUtil.doGet(sina_api_url + base64Url);
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
			System.out.println("---------->22 " + long_url);
			return long_url;
		}
		if (StringUtil.isEmpty(ret)) {
			return long_url;
		}
		String retUrl = null;
		try {
		    //这里不用JSONOBJECT进行转换取值,是因为新浪返回值包含原long_url，这个long_url是多样的，经常会出现不可预料的转义字符导致转换JSONOBJECT失败，
			//所以这里采用截取字符串处理,也可以使用正则表达式处理
			//例如: https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_16269948374794545345429716%22%7D&n_type=0&p_from=1
			if(StringUtils.areNotEmpty(ret) && ret.indexOf("url_short\":") >= 0) {
				retUrl = ret.substring(ret.indexOf("url_short\":") + 12 , (ret.length()-1));
				retUrl = retUrl.substring(0, retUrl.indexOf("\"")) ;
				return retUrl;
			}
		} catch (Exception e) {
			return long_url;
		}
		if(StringUtils.isEmpty(retUrl)) {
			return long_url;
		}
		return retUrl;
	}
	
	
	public static void main(String args[]) throws Exception {
		String localUrl = "http://h5.xzhuang.com/goods/index.html?talentShareGoodId=1248652&goodId=2938&talentId=938534&from=singlemessage&pid=41";
		
		
		//http%3A%2F%2Fh5.xzhuang.com%2Fgoods%2Findex.html%3FtalentShareGoodId%3D1248652%26goodId%3D2938%26talentId%3D938534%26from%3Dsinglemessage
		//System.out.println(localUrl +  " ====================== "  + generateSinaShortUrl(localUrl));
		
		System.out.println(generateSinaShortUrl(localUrl));
		
		
		//System.out.println(generateSinaShortUrl("https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_15958884071310657535%22%7D&n_type=0&p_from=1"));
		
		
		
		
		
		
		
		//System.out.println(generateBaiDuShortUrl("https://www.zhihu.com/question/22041107?rf=20039685"));
		
	}
}
